package com.xuy.ridis.redisdemo.JvmDemo;

import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class LockService {
    private static JedisPool pool = null;

    static {
        JedisPoolConfig config = new JedisPoolConfig();
        // 设置最大连接数
        config.setMaxTotal(200);
        // 设置最大空闲数
        config.setMaxIdle(8);
        // 设置最大等待时间
        config.setMaxWaitMillis(1000 * 100);
        // 在borrow一个jedis实例时，是否需要验证，若为true，有jedis实例均是可用的
        config.setTestOnBorrow(true);
        pool = new JedisPool(config, "192.168.130.129", 6379, 3000);
    }

    RedisLock lockRedis = new RedisLock(pool);

    public void seckill() {
        String identifier = lockRedis.lockWithTimeout("itmayiedu", 5000L, 5000L);
        if (StringUtils.isEmpty(identifier)) {
            // 获取锁失败
            System.out.println(Thread.currentThread().getName() + ",获取锁失败，原因时间超时!!!");
            return;
        }
        System.out.println(Thread.currentThread().getName() + "获取锁成功,锁id identifier:" + identifier + "，执行业务逻辑");
        try {
            Thread.sleep(30);
        } catch (Exception e) {

        }
        // 释放锁
        boolean releaseLock = lockRedis.releaseLock("itmayiedu", identifier);
        if (releaseLock) {
            System.out.println(Thread.currentThread().getName() + "释放锁成功,锁id identifier:" + identifier);
        }
    }


    static class ThreadRedis extends Thread {
        private LockService lockService;

        public ThreadRedis(LockService lockService) {
            this.lockService = lockService;
        }

        @Override
        public void run() {
            lockService.seckill();

        }

    }

    public static class Test001 {

        public static void main(String[] args) {
            LockService lockService = new LockService();
            for (int i = 0; i < 50; i++) {
                ThreadRedis threadRedis = new ThreadRedis(lockService);
                threadRedis.start();
            }
        }

    }

}

